package com.gqb.spring.framework;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import org.xml.sax.SAXException;

public class FacadeBeanFactory extends AbstractBeanFactory{
	private XmlBeanFactory xmlBeanFactory;
	private AnnotationBeanFactory annotationBeanFactory;
	private String xmlName;
	public FacadeBeanFactory(String xmlPath) throws ClassNotFoundException, IOException, SAXException {
		super();
		this.xmlName = xmlPath;
		load();
	}
	
	private void refresh() throws IOException, SAXException, ClassNotFoundException{
		xmlBeanFactory=new XmlBeanFactory(this,xmlName);
		xmlBeanFactory.load();
		annotationBeanFactory=new AnnotationBeanFactory(this);
		annotationBeanFactory.load();
	}

	@Override
	public Object getBean(String beanId) throws ClassNotFoundException,
			InstantiationException, IllegalAccessException,
			NoSuchMethodException, SecurityException, IllegalArgumentException,
			InvocationTargetException {
		Object object = getBeanIdObject().get(beanId);
		if (null != object)
			return object;
		object = xmlBeanFactory.getBean(beanId);
		if (null != object) {
			InitializingBeanProcessor.Process(object,
					getBeanIdBean().get(beanId), beanIdClass.get(beanId));
			return object;
		}
		object = annotationBeanFactory.getBean(beanId);
		InitializingBeanProcessor.Process(object, beanIdBean.get(beanId),
				beanIdClass.get(beanId));
		return object;
	}

	@Override
	public void load() throws IOException, SAXException, ClassNotFoundException {
		refresh();
	}
	
}
